package com.banmao.youZone.entity;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.banmao.youZone.entity.login.A00info;
import com.banmao.youZone.entity.login.AccessTokenDto;
import com.banmao.youZone.entity.login.BaseUserInfo;
import com.banmao.youZone.entity.login.QzBaseDTO;
import com.banmao.youZone.util.AesUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("request_property")
public class RequestProperty {

    @TableId("userId")
    private Integer userId;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField("update_date")
    private Date updateDate;

    @TableField("accessToken")
    @NotBlank(message = "[accessToken]不能为空")
    private String accessToken;

    @TableField("sn")
    @NotBlank(message = "[sn]不能为空")
    private String sn;

    @TableField("a00")
    @NotBlank(message = "[a00]不能为空")
    private String a00;

    @TableField("encrypted_user_id")
    @NotBlank(message = "[encrypted_user_id]不能为空")
    private String encryptedUserId;

    @TableField("yht_access_token")
    @NotBlank(message = "[yht_access_token]不能为空")
    private String yhtAccessToken;

    @TableField("ytenantId")
    @NotBlank(message = "[ytenantId]不能为空")
    private String ytenantId;

    @TableField("qz_id")
    @NotBlank(message = "[qz_id]不能为空")
    private String qzId;

    @TableField("defaultLocale")
    @Builder.Default
    private String defaultLocale = "zh_CN";

    @TableField("locale")
    @Builder.Default
    private String locale = "zh_CN";

    @TableField("orgId")
    @Builder.Default
    private String orgId = "";

    @TableField("defaultOrg")
    @Builder.Default
    private String defaultOrg = "";

    @TableField("language")
    @Builder.Default
    private String language = "001";

    @TableField("languages")
    @Builder.Default
    private String languages = "1_3-2_1-3_1";

    @TableField("timezone")
    @Builder.Default
    private String timezone = "UTC+08:00";

    @JsonProperty("nFf")
    @TableField("n_f_f")
    @Builder.Default
    private String nFf = "true";

    @TableField("newArch")
    @Builder.Default
    private String newArch = "true";

    @TableField("multilingualFlag")
    @Builder.Default
    private String multilingualFlag = "true";

    @TableField("vercode")
    @Builder.Default
    private String vercode = "1-7.6.17-1-1";

    @TableField("imei")
    private String imei;

    @TableField("deviceModel")
    private String deviceModel;

    @TableField("deviceName")
    private String deviceName;

    @TableField("userAgent")
    private String userAgent;

    public static RequestPropertyBuilder builder() {
        return new RequestPropertyBuilder();
    }

    public static RequestPropertyBuilder builder(String jsonStr) {

        LoginResponse loginResponse = JSON.parseObject(jsonStr, LoginResponse.class);
        if (loginResponse == null) {
            throw new IllegalArgumentException("JSON解析失败");
        }

        A00info a00info = loginResponse.getData().getA00info();
        AccessTokenDto accessTokenDto = loginResponse.getData().getAccessTokenDto();
        QzBaseDTO qzBaseDTO = loginResponse.getData().getQzBaseDTO();
        BaseUserInfo baseUserInfo = loginResponse.getData().getBaseUserInfo();

        RequestPropertyBuilder RequestPropertyBuilder = new RequestPropertyBuilder();

        if (baseUserInfo != null) {
            RequestPropertyBuilder.encryptedUserId(AesUtil.aesEncode(AesUtil.SIGN_KEY, baseUserInfo.getUserId()));
        }

        if (a00info != null) {
            RequestPropertyBuilder.a00(a00info.getA00())
                    .orgId(a00info.getOrgId())
                    .defaultOrg(a00info.getDefaultOrg())
                    .language(a00info.getLanguage())
                    .languages(a00info.getLanguages())
                    .locale(a00info.getLocale())
                    .defaultLocale(a00info.getDefaultLocale())
                    .timezone(a00info.getTimezone())
                    .multilingualFlag(a00info.getMultilingualFlag())
                    .nFf(a00info.getN_f_f())
                    .newArch(a00info.getNewArch());
        }

        if (accessTokenDto != null) {
            RequestPropertyBuilder.accessToken(accessTokenDto.getAccessToken())
                    .sn(accessTokenDto.getSn());
        }

        if (qzBaseDTO != null) {
            RequestPropertyBuilder.qzId(String.valueOf(qzBaseDTO.getQzId()))
                    .ytenantId(qzBaseDTO.getYtenantId());
        }
        RequestPropertyBuilder.yhtAccessToken(loginResponse.getData().getYhtAccessToken());

        return RequestPropertyBuilder;
    }

    public static RequestPropertyBuilder clockParam(String param) {
        Map<String, String> paramMap = HttpUtil.decodeParamMap(param, StandardCharsets.UTF_8);

        if (!paramMap.containsKey("encryptedAttentance")) {
            throw new IllegalArgumentException("未包含 encryptedAttentance 参数");
        }

        String encryptedAttentance = paramMap.get("encryptedAttentance");

        String decrypt = AesUtil.aesDecode(AesUtil.SIGN_KEY, encryptedAttentance);

        RequestProperty rt = JSON.parseObject(decrypt, RequestProperty.class);
        if (rt == null) {
            throw new IllegalArgumentException("encryptedAttentance JSON解析失败");
        }

        RequestPropertyBuilder builder = new RequestPropertyBuilder();
        builder.imei(rt.getImei())
                .deviceModel(rt.deviceModel)
                .deviceName(rt.deviceName);

        return builder;

    }

}
